; NAME:
; gauge_flux_mozart_combine
;
; PURPOSE:
; combines the regridded output for overlapping time periods
;
; CATEGORY:
; Regridding
;
; CALLING SEQUENCE:
; combine_gauge_flux_mozart, INPUTFILES = InputFiles, OUTPUTFILE = OutputFile
;
; INPUTS:
; TRACERNAMES : A string array listing the tracers you want to combine
; INPUTFILES : Input files to be combined
;              a hash with keys TRACERNAMES
; FREQ : Frequency you want the output in
;        Defaults to monthly
; STARTY : Start year defaults to 2003
; ENDY : End year defaults to 2011
;
; OUTPUTS:
; Creates a netcdf file for each
;
; SIDE EFFECTS:
;
; RESTRICTIONS:
; Flux filenames must be of the form
; FluxName_*_YYYYMM_MZT.nc
;
; PROCEDURE:
;
; MODIFICATION HISTORY:
;   Written 27/2/2014 by as13988
;

Pro gauge_flux_mozart_combine, TRACERNAMES = TracerNames, INPUTFILES = InputFiles, FREQ = Freq, $
  STARTY = StartY, ENDY = EndY, SPECIES = Species
  
  
  ; Check inputs
  ;----------------------------------------------------------------------------------------------------------------
  If keyword_set(SPECIES) eq 0 then begin
    Species = 'CO2'
    Print, 'Defaulting to using CO2 as the species when naming the file'
    
  Endif
  
  
  if n_elements(INPUTFILES) eq 0 then begin                                                 ; Input files not given
  
    NoTracerNames_Flag = 0                 ; Set default flag
    
    If (n_elements(TRACERNAMES) eq 0) and (size(TRACERNAMES, /type) ne 7) then begin        ; tracer names not given
      NoTracers = ''
      
      ; find out the number of tracers
      read, NoTracers, prompt = 'How many tracers are there?'
      
      NoTracers = fix(NoTracers)
      TracerNames = strarr(NoTracers)
      
      NoTracerNames_Flag = 1             ; set flag
      
    Endif                                                                                   ; End tracer names not given
    
    
    InputFiles = hash()
    
    ; Need to input files/tracer names
    for i = 0L, NoTracers-1 do begin
    
      InputFiles_i = dialog_pickfile(path = '/data/shared/GAUGE/MOZART/', filter = '*_MZT.nc', /MULTIPLE_FILES, $
        TITLE='Please select the files for tracer ' + strtrim(i+1, 2))
        
      if NoTracerNames_Flag eq 1 then begin
        TracerName_i = (strsplit((strsplit(InputFiles_i[0], '\/.', /extract))[-2], '_',  /extract))[0]
        TRACERNAMES[i] = TracerName_i
      endif
      
      InputFiles[TracerNames[i]] =InputFiles_i
      
    endfor
    
    
  endif else begin                                           ; End input files not given
  
    ; Check if the input files are in the correct format
    ; Check that the inputfiles input is a Hash
    Help, InputFiles, OUTPUT = InputFiles_Help
    
    if total(strmatch(InputFiles_Help, '*HASH*')) lt 1 then begin
      Print, 'The input files are not given in a hash as required'
      Return
      
    endif
    
    if n_elements(TracerNames) eq 0 then TracerNames = (inputfiles.keys()).toarray()
    
    Keys =  (inputfiles.keys()).toarray()
    
    if array_equal(TracerNames[sort(tracernames)], Keys[sort(keys)]) eq 0 then begin
      print, 'The keys associated with the input file hash do not match the given tracer names'
      return
    endif
    
    
  endelse
  ;----------------------------------------------------------------------------------------------------------------
  
  
  ; Create output times
  ; ---------------------------------------------------------------------------------------------------------------
  ; Set start and end defaults
  if n_elements(StartY) eq 0 then StartY = 2003
  if n_elements(EndY) eq 0 then EndY = 2011
  NoYears = EndY - StartY + 1
  
  ; Set freqency default
  if n_elements(freq) eq 0 then FREQ = 'Monthly'
  
  ; Set up inputs for timegen
  FreqOptions = ['Monthly', '3hrly', 'Daily']
  
  key = where(strmatch(FreqOptions, Freq, /FOLD_CASE))
  
  case (key) of
    0: begin
      ; monthly
      Units = 'MONTH'
      Start = julday(1, 15, startY, 0, 0, 0)
      N_Times = ((endY - startY + 1)*12)
      
    end
    
    1: begin
      ; 3hrly
      Hours = [0,3,6,9,12,15,18,21]
      Start = julday(1, 1, StartY, 0, 0, 0)
      N_Times = ((julday(1, 1, EndY+1, 0, 0, 0) - julday(1, 1, startY, 0, 0, 0))*8)
      
    end
    
    2: begin
      ; Daily
      Units = 'DAY'
      Start = julday(1, 1, StartY, 0, 0, 0)
      N_Times = ((julday(1, 1, EndY+1, 0, 0, 0) - julday(1, 1, startY, 0, 0, 0)))
      DAILY = 1
    end
    
    else: begin
      print, 'Can not match frequency'
      Return
      
    end
  endcase
  
  ; Create a JD of the out times
  OutTime=timegen(N_Times, start = start, Hours = Hours, units=units)
  CalDat, OutTime, OutMonth, OutDay, OutYear, OutHour
  OutDate = mr_mozdate(outTime)
  OutDays = outTime - julday(1, 121, 1, 0)
  ; ---------------------------------------------------------------------------------------------------------------
  
  
  ; Create an array to store the separate tracer data for the previous year
  n_lon = 144 ;
  n_lat = 96 ;
  NoTracers = N_Elements(TracerNames)
  
  OutputFiles = !null
  
  ; Loop through each year
  ; Loop through each tracer
  ; Average if necessary and write the file
  ; ---------------------------------------------------------------------------------------------------------------
  for i = 0L, NoYears-1 do begin
  
    Year_i = StartY + i
    
    print, 'Processing data for ' + strtrim(Year_i, 2)
    
    OutDates_i = OutDate[where(OutYear eq Year_i)]
    OutDays_i = OutDays[where(OutYear eq Year_i)]
    
    TracerData = !null
    
    
    for j = 0L, NoTracers-1 do begin
    
      ; Extract All the tracer files for tracer j
      TracerFiles = InputFiles[TracerNames[j]]
      
      ; If there's only a single file than the below will fail
      ; reform it to the correct shapped array
      
      
      ; Find the files for Year i
      
      ; this is the work around for a single file
      ; rather than yearly files
      if N_Elements(TracerFiles) gt 1 then begin
        FileYears = strmid(((strsplit(TracerFiles, '_.', /EXTRACT)).toarray())[*,-3], 0, 4)
        
        
        Print, 'Processing ' + TracerNames[j] + ' ' +  strtrim(Year_i, 2)
        
        TracerFiles_i = TracerFiles[where(FileYears eq Year_i, count)]
        
        if count eq 0 then begin
        
          print, 'Can not find matching files for '+ TracerNames[j]+ ' in ' + strtrim(StartY+i, 2)
          
          ;Use max year instead
          print, 'Defaulting to ' + strtrim(Max(FileYears), 2)
          
          TracerFiles_i = TracerFiles[where(FileYears eq max(FileYears), count)]
          
          Year_i = Max(fileYears)
        endif
        
      endif else begin
        TracerFiles_i = TracerFiles
      endelse
      
      ; Read in the files
      Read_MZT_File_Multi, FILES = TracerFiles_i, Tracerj_Datai, FLUX_NCNAME = '*emissions', DATESEC_NCNAME = 'time'
      
      ; if required average/expand the files
      ; Find the outdays for the given year
      OutDays_j = OutDays[where(OutYear eq Year_i)]
      
      
      
      
      ; Check if the time steps in the read in files match that in the output array
      if ((abs(mean(OutDays_j - Tracerj_Datai.time)) lt 0.001) and $
        (N_elements(Outdays_j) eq n_elements(tracerj_datai.time) ) ) or $
        ((abs(mean(OutDays_j - (Tracerj_Datai.time+14))) lt 0.001) and $ ; adjusting for 1st or the 15th of the month
        (N_elements(Outdays_j) eq n_elements(tracerj_datai.time) ) ) $
        then begin
        
        ; Time steps match no need to average/expand
        Fluxj_datai = Tracerj_Datai.flux
        
      endif else begin
        ; Extract the in data for the given year
        InJD = mr_mozdate(Tracerj_Datai.date)
        
        CalDat, InJD, InMonth, InDay, InYear
        
        Year_index = where(InYear eq Year_i, count)
        
        if count eq 0 then begin
          ;instead default to the closest year
          Year_i = InYear[(where( ABS(Inyear - year_i) eq min(abs(inYear - Year_i)) ))[0]]
          print, 'Defaulting to closest year, ' + strtrim(Year_i, 2) + ', for ' + Tracernames[j]
          Year_index = where(InYear eq Year_i, count) ; need to recalculate the year index using the new year_i
        endif
        
        ; time stamps don't match need to average/replicate flux
        gauge_reform_flux, (tracerj_datai.date)[Year_index], (tracerj_datai.time)[Year_index], $
          (Tracerj_Datai.Flux)[*,*,Year_index], OutDates_i, OutDays_i, Fluxj_Datai, StartY+i, DAILY =daily
        
      endelse
      
      
      
      ; Calculate the sum
      help, sum, OUTPUT= help_sum
      if  strmatch(Help_sum, '*UNDEFINED*') eq 1 then Sum =  Fluxj_datai else Sum = Sum + Fluxj_datai
      
      undefine, fluxj_datai
      
    endfor ; Tracer loop
    
    
    ; Make a directory to put the data in if needed
    cd, '/data/shared/GAUGE/MOZART/' + SPECIES +'/', current = current
    if file_test( strtrim(StartY + i, 2), /DIRECTORY) eq 0 then File_MKDIR, strtrim(StartY + i, 2)
    cd, current
    
    ; Write the file
    ncName = 'emissions.' + species + '.surface.MERRA.' + freq + '.nc'
    ncDir = '/data/shared/GAUGE/MOZART/' + SPECIES +'/' + strtrim(StartY+i, 2) +'/'
    
    print, 'Writing: ' + NcDir + ncname
    
    WRITE_MOZART_NC, Tracerj_Datai.lat, Tracerj_Datai.lon, sum, OutDates_i, OutDays_i, SPECIES, ncName, ncDir
    
    OutputFiles = [OutputFiles, ncDir+ncName]
    
    undefine, sum
    
  endfor ; Year loop
  
  ; Now go back and append a data point at the start and end of each file
  AppendStartAndEnd_Mozart_NC, OutputFiles, SPECIES=SPECIES
  
End